package com.libre.workflow.app;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.libre.common.utils.StringUtil;
import org.flowable.engine.ManagementService;
import org.flowable.idm.api.Group;
import org.flowable.idm.api.IdmIdentityService;
import org.flowable.idm.api.User;
import org.flowable.ui.common.model.GroupRepresentation;
import org.flowable.ui.common.model.ResultListDataRepresentation;
import org.flowable.ui.common.model.UserRepresentation;
import org.flowable.ui.common.security.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
* @description
* @author        庞留杰
* @date          2020/9/10 10:29
* @version       V1.0
**/
@RestController
@RequestMapping("/app")
public class RemoteAccountResource {

    @Autowired
    protected IdmIdentityService idmIdentityService;
    @Autowired
    protected ManagementService managementService;

    /**
     * 功能描述: 查询当前用户信息
     * @author: 庞留杰
     * @exception
     * @date: 2020/9/10 10:30
     */
    @GetMapping(value = "/rest/account", produces = "application/json")
    public UserRepresentation getAccount() {
        User remoteUser = SecurityUtils.getCurrentUserObject();
        //构建用户代表类
        UserRepresentation userRepresentation = new UserRepresentation(remoteUser);
        SecurityUtils.assumeUser(remoteUser); //保证创建流程可用
        return userRepresentation;
    }

    /**
     * 功能描述: 用于Flowable Modeler分组下拉
     * @author: 庞留杰
     * @exception
     * @date: 2020/9/9 17:01
     */
    @RequestMapping(value = "/rest/editor-groups", method = RequestMethod.GET)
    public ResultListDataRepresentation getGroups(@RequestParam(required = false, value = "filter") String filter) {
         /* 查询FLowable自带表act_id_user
            if (StringUtils.isNotBlank(filter)) {
                filter = filter.trim();
                String sql = "select * from act_id_group where NAME_ like #{name}";
                filter = "%" + filter + "%";
                List<Group> groups = idmIdentityService.createNativeGroupQuery().sql(sql).parameter("name", filter).listPage(0, 10);
                List<GroupRepresentation> result = new ArrayList<>();
                for (Group group : groups) {
                    result.add(new GroupRepresentation(group));
                }
                return new ResultListDataRepresentation(result);
            }
            return null;
        */
        if (StringUtil.isNotEmpty(filter)) {
            filter = filter.trim();
            String sql = "SELECT sr.id AS ID_,sr.role_name AS NAME_,sr.code AS TYPE_ FROM sys_role sr WHERE sr.role_name LIKE #{name}";
            filter = "%" + filter + "%";
            List<Group> groups = idmIdentityService.createNativeGroupQuery().sql(sql).parameter("name", filter).list();
            List<GroupRepresentation> result = new ArrayList<>();
            for (Group group : groups) {
                result.add(new GroupRepresentation(group));
            }
            return new ResultListDataRepresentation(result);
        }
        return null;
    }

    /**
     * 功能描述: 用于Flowable Modeler用户下拉
     * @author: 庞留杰
     * @exception
     * @date: 2020/9/9 17:01
     */
    @RequestMapping(value = "/rest/editor-users", method = RequestMethod.GET)
    public ResultListDataRepresentation getUsers(@RequestParam(value = "filter", required = false) String filter) {
       /* 查询FLowable自带表act_id_user
            if (StringUtils.isNotBlank(filter)) {
                filter = filter.trim();
                String sql = "select * from act_id_user where ID_ like #{id} or LAST_ like #{name}";
                filter = "%"+filter+"%";
                List<User> matchingUsers = idmIdentityService.createNativeUserQuery().sql(sql).parameter("id",filter).parameter("name",filter).listPage(0, 10);List<UserRepresentation> userRepresentations = new ArrayList<>(matchingUsers.size());
                for (User user : matchingUsers) {
                    userRepresentations.add(new UserRepresentation(user));
                }
                return new ResultListDataRepresentation(userRepresentations);
            }
            return null;
        */
        if (StringUtils.isNotEmpty(filter)) {
            filter = filter.trim();
            String sql = "SELECT su.id AS ID_,su.real_name  AS FIRST_,su.code  AS LAST_ ,CONCAT(su.real_name,'【',su.code,'】') AS DISPLAY_NAME  FROM sys_user su WHERE su.real_name LIKE #{name} OR su.code LIKE #{name}";
            filter = "%"+filter+"%";
            List<User> matchingUsers = idmIdentityService.createNativeUserQuery().sql(sql).parameter("id",filter).parameter("name",filter).listPage(0, 10);List<UserRepresentation> userRepresentations = new ArrayList<>(matchingUsers.size());
            for (User user : matchingUsers) {
                userRepresentations.add(new UserRepresentation(user));
            }
            return new ResultListDataRepresentation(userRepresentations);
        }
        return new ResultListDataRepresentation(new ArrayList<>());
    }

}
